#include	<ncurses.h>

#include	"EventManager.hpp"

EventManager::EventManager(WindowCode *windowCode, WindowExplorer *winExplorer, WindowWarning *winWarning)
  :_focus(FOCUS_WINDOW_CODE), _windowCode(windowCode), _winExplorer(winExplorer), _winWarning(winWarning)
{
  
}

EventManager::~EventManager()
{
  
}

void	EventManager::manageKeyTab()
{
  if (this->_focus == FOCUS_WINDOW_CODE)
    this->_focus = FOCUS_WINDOW_EXPLORER;
  else if (this->_focus == FOCUS_WINDOW_EXPLORER)
    this->_focus = FOCUS_WINDOW_CODE;
}

void	EventManager::manageKeyArrow(int ch)
{
  if (this->_focus == FOCUS_WINDOW_CODE)
    {
      this->_windowCode->checkEvent(ch);
      this->_winWarning->checkEvent(ch);
    }
  else if (this->_focus == FOCUS_WINDOW_EXPLORER)
    this->_winExplorer->checkEvent(ch);
}

void	EventManager::manageKeyLeftRight(int ch)
{
  this->_windowCode->checkEvent(ch);
}

bool	EventManager::getNextEvent()
{
  int	ch;

  ch = 0;
  if (this->_focus == FOCUS_WINDOW_CODE)
    ch = wgetch(this->_windowCode->getWindow());
  else if (this->_focus == FOCUS_WINDOW_EXPLORER)
    ch = wgetch(this->_winExplorer->getWindow());
  else if (this->_focus == FOCUS_WINDOW_WARNING)
    ch = wgetch(this->_winWarning->getWindow());
  if (ch == 9) //TAB
    {
      manageKeyTab();
      return (true);
    }
  else if (ch == KEY_UP || ch == KEY_DOWN)
    {
      manageKeyArrow(ch);
      return (false);
    }
  else if (ch == KEY_RIGHT || ch == KEY_LEFT)
    {
      manageKeyLeftRight(ch);
      return (true);
    }
  else if (ch == '\n' && this->_focus == FOCUS_WINDOW_EXPLORER)
    {
      this->_windowCode->setContentWin(this->_winExplorer->getLineOnCursor().c_str());
      this->_winWarning->setContentWin(this->_winExplorer->getLineOnCursor().c_str(), this->_windowCode->getSubWinCode()->getContentTree()->getList().size());
      return (true);
    }
}

int	EventManager::getFocusState()const
{
  return (this->_focus);
}
